import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class ThreadPoolExample {
    public static void main(String[] args){
        //固定线程池
        ExecutorService fixedThreadPool= Executors.newFixedThreadPool(3);
        for(int i=0;i<5;i++){
            fixedThreadPool.submit(()->System.out.println(Thread.currentThread().getName()));
        }
        //缓存线程池
        ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
        for (int i = 0; i < 5; i++) {
            cachedThreadPool.submit(() -> System.out.println(Thread.currentThread().getName() ));
        }
        //单线程池
        ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();
        for (int i = 0; i < 5; i++) {
            singleThreadExecutor.submit(() -> System.out.println(Thread.currentThread().getName()));
        }
        //定时线程池
        ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(2);
        scheduledThreadPool.scheduleAtFixedRate(() -> System.out.println(Thread.currentThread().getName()), 0, 1, TimeUnit.SECONDS);
    }

}
